package com.net.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;


import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String C_YYYY_MM = "yyyy年MM月";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDD = "yyyyMMdd";

    public static String MM_DD = "MM.dd";

    public static String MMDD = "MMdd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    private final static String[] PARSE_PATTERNS = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy-MM-dd HH:mm:ss.SSS", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM", "yyyy.MM.dd HH:mm:ss.SSS"};

    /**
     * 1秒钟
     */
    private static final long MILLIS_IN_SECOND = 1000;

    /**
     * 1分钟
     */
    private static final long MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND;

    /**
     * 1个小时
     */
    private static final long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
    /**
     * 1天
     */
    private static final long MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
    /**
     * 1个月
     */
    private static final long MILLIS_IN_MONTH = 31 * MILLIS_IN_DAY;

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static String parseDateToStr(final String format, final Date date) {
        return DateFormatUtils.format(date, format);
    }

    public static Date dateTime(final String format, final String ts) {
        if (StringUtils.isBlank(ts)) {
            return null;
        }
        try {
            return parseDate(ts, format);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static String datePath() {
        return DateFormatUtils.format(new Date(), "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static String dateTime() {
        return DateFormatUtils.format(new Date(), "yyyyMMdd");
    }


    public static Date addTime(long ms) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(System.currentTimeMillis() + ms);
        return instance.getTime();
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), PARSE_PATTERNS);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 获取指定日期的00:00:00或23:59:59
     *
     * @param date
     * @param zero
     * @return java.lang.String
     * @author lizhi
     **/
    public static String getDateZeroOrTwelve(Date date, Boolean zero) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (zero) {
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
        }
        return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, calendar.getTime());
    }

    /**
     * 根据小时差获取过去或将来时间
     *
     * @param date
     * @param hours
     * @param past
     * @return java.util.Date
     * @author lizhi
     **/
    public static String getDateByDiffHours(Date date, Integer hours, boolean past) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (past) {
            calendar.setTimeInMillis(date.getTime() - hours * 60 * 60 * 1000);
        } else {
            calendar.setTimeInMillis(date.getTime() + hours * 60 * 60 * 1000);
        }
        return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, calendar.getTime());
    }


    public static boolean compare(Date collectionDate, Date expirationDate) {
        return expirationDate.before(getNowDate()) || expirationDate.before(collectionDate);

    }

    /**
     * 计算当前日期与{@code endDate}的间隔天数
     *
     * @param endDate
     * @return 间隔天数
     */
    public static Long getDatePoors(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;

        return day;
    }

    /**
     * 计算两个时间的间隔分钟
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDatePoorsForMinute(Date startDate, Date endDate) {
        //每天毫秒数
        long nd = 1000 * 24 * 60 * 60;
        Calendar calendarOne = Calendar.getInstance();
        Calendar calendarTwo = Calendar.getInstance();
        calendarOne.setTime(startDate);
        calendarTwo.setTime(endDate);
        int year = calendarTwo.get(Calendar.YEAR) - calendarOne.get(Calendar.YEAR);
        int month = calendarTwo.get(Calendar.MONTH) + year * 12 - calendarOne.get(Calendar.MONTH);
        int day = calendarTwo.get(Calendar.DATE) + month * 30 - calendarOne.get(Calendar.DATE);
        int hour = calendarTwo.get(Calendar.HOUR) + day * 24 - calendarOne.get(Calendar.HOUR);
        return calendarTwo.get(Calendar.MINUTE) + hour * 60 - calendarOne.get(Calendar.MINUTE);
    }

    /**
     * 计算两个日期相差的月份
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 两个日期相差的月份
     */
    public static int discrepancyDate(Date startDate, Date endDate) {
        Calendar calendarOne = Calendar.getInstance();
        Calendar calendarTwo = Calendar.getInstance();
        calendarOne.setTime(startDate);
        calendarTwo.setTime(endDate);
        int year = calendarTwo.get(Calendar.YEAR) - calendarOne.get(Calendar.YEAR);
        return calendarTwo.get(Calendar.MONTH) + year * 12 - calendarOne.get(Calendar.MONTH);
    }


    /**
     * 判断某年是否为闰年
     *
     * @param year 年份
     * @return boolean
     */
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
    }

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(Date date) {
        if (date == null) {
            return "";
        }

        long diff = System.currentTimeMillis() - date.getTime();

        if (diff > MILLIS_IN_MONTH) {
            return parseDateToStr(YYYY_MM_DD, date);
        }

        if (diff > MILLIS_IN_DAY * 2) {
            return (diff / (MILLIS_IN_DAY)) + "天前";
        }

        if (diff > MILLIS_IN_DAY) {
            return "昨天";
        }

        if (diff > MILLIS_IN_HOUR) {
            return (diff / MILLIS_IN_HOUR) + "小时前";
        }

        if (diff > MILLIS_IN_MINUTE) {
            return (diff / MILLIS_IN_MINUTE) + "分钟前";
        }

        return "刚刚";
    }

    /**
     * ************************************************************************
     * 返回时间差
     *
     * @param strDateBegin
     * @param beginFormat
     * @param strDateEnd
     * @param endFormat
     * @param iCalendarType
     * @return int
     * @throws ParseException
     */
    public static int diffDate(String strDateBegin, String beginFormat, String strDateEnd, String endFormat, int iCalendarType) throws ParseException {
        Calendar calBegin = getCalendarInstance(strDateBegin, beginFormat);
        Calendar calEnd = getCalendarInstance(strDateEnd, endFormat);
        long lBegin = calBegin.getTimeInMillis();
        long lEnd = calEnd.getTimeInMillis();
        Long lSS = lEnd - lBegin;

        int result = 0;

        switch (iCalendarType) {

            case Calendar.YEAR:
            case Calendar.MONTH:
            case Calendar.DAY_OF_YEAR:
                throw new UnsupportedOperationException();
            case Calendar.DATE:
                result = (int) (lSS / MILLIS_IN_DAY);
                break;
            case Calendar.HOUR_OF_DAY:
                result = (int) (lSS / MILLIS_IN_HOUR);
                break;
            case Calendar.MINUTE:
                result = (int) (lSS / MILLIS_IN_MINUTE);
                break;
            case Calendar.SECOND:
                result = (int) (lSS / MILLIS_IN_SECOND);
                break;
            case Calendar.MILLISECOND:
                break;// never useful
            default:
                throw new IllegalArgumentException("日历参数 " + iCalendarType + " 是不被支持");
        }
        return result;
    }

    /**
     * 获得日历对象
     *
     * @param baseDate
     * @param baseFormat
     * @return Calendar
     * @throws ParseException
     */
    private static Calendar getCalendarInstance(String baseDate, String baseFormat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(baseFormat);
        Date date = sdf.parse(baseDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}
